 $(function () {
     var upload = function (url) {
         //console.log("bufu")
         var uploader = WebUploader.create({
             server: url,
             pick: '#picker',
             runtimeOrder: 'html5',
             auto: true,
             fileVal: 'excel',
             fileSingleSizeLimit: '20971520',
             fileSizeLimit: '20971520',
             accept: {
                 title: 'file',
                 extensions: 'xlsx,xls',
                 mimeTypes: '.xlsx,.xls' //则打开窗口中只显示该后缀的文件
             },
             duplicate: true
         });
         uploader.on('fileQueued', function (file) {
             $('#thelist').append(
                 '<div id="' + file.id + '" class="alert alert-info alert-dismissible" role="alert">' +
                 '<button type="button" class="close" data-dismiss="alert" aria-label="Close">' +
                 '<span aria-hidden="true">&times;</span></button>' +
                 file.name + '<span class="state">等待上传...<span>' +
                 '</div>');
         });
         //生成进度条
         uploader.on('uploadProgress', function (file, percentage) {
             var $li = $('#' + file.id),
                 $percent = $li.find('.progress .progress-bar');
             // 避免重复创建
             if (!$percent.length) {
                 $percent = $('<div class="progress progress-striped active">' +
                     '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                     '</div>' +
                     '</div>').appendTo($li).find('.progress-bar');
             }
             $('#' + file.id).removeClass("alert-success alert-danger").addClass("alert-success")
             $li.find('.state').text('上传中');
             $percent.css('width', percentage * 100 + '%');
         });
         //上传成功
         uploader.on('uploadSuccess', function (file, response) {
             if (response.status == 1) {
                 $('#' + file.id).removeClass("alert-info alert-danger").addClass("alert-success")
                 $('#' + file.id).find('.state').text('已上传');
             } else {
                 $('#' + file.id).removeClass("alert-info alert-success").addClass("alert-danger")
                 $('#' + file.id).find('.state').text('上传出错,未提交到数据库');
             }
         });
         //上传出错
         uploader.on('uploadError', function (file, reason) {
             console.log(reason);
             $('#' + file.id).removeClass("alert-info alert-success").addClass("alert-danger")
             $('#' + file.id).find('.state').text('上传出错，上传到服务器出错');
         });

         uploader.on('uploadComplete', function (file) {
             $('#' + file.id).find('.progress').fadeOut();
             setTimeout(function () {
                 $('#' + file.id).remove();
             }, 3000);
         });
         uploader.on('error', function (handler) {
             alert("错误，" + handler)
         });
     }
     window.upload = upload;
 });